//
//  KYBaseDataTool.m
//  KYKJPackDemo
//
//  Created by Jordan zhou on 2017/9/28.
//  Copyright © 2017年 ???. All rights reserved.
//

#import "KYBaseDataTool.h"

@implementation KYBaseDataTool
#pragma mark - 时间字符串转毫秒字符串
+ (NSString *)timeTurnMilliseconds:(NSString *)timeStr
{
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *date = [dateFormatter dateFromString:timeStr];
    NSTimeInterval timeInterval=[date timeIntervalSince1970] * 1000;
    
    return [NSString stringWithFormat:@"%0.0f",timeInterval];
}

#pragma mark - 根据传进来的计算时间字符
+ (NSString *)timeOfString:(NSString *)timeString
{
    if ([timeString isEqualToString:@"0"]|| [timeString isEqualToString:@"请选择"]) {
        return @"";
    }
    
    long long value = [timeString doubleValue]/1000.0f;
    
    if (value == 0) {
        return @"";
    }
    
    NSNumber *time = [NSNumber numberWithLongLong:value];
    NSTimeInterval nsTimeInterval = [time longValue];
    NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:nsTimeInterval];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    return [dateFormatter stringFromDate:date];
}

#pragma mark - 手机号码验证
+ (BOOL)isMobileNumber:(NSString *)mobileNum
{
    /**
     * 手机号码
     * 移动：134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     * 联通：130,131,132,152,155,156,185,186
     * 电信：133,1349,153,180,189
     */
    NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
    /**
     10 * 中国移动：China Mobile
     11 * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     12 */
    NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
    /**
     15 * 中国联通：China Unicom
     16 * 130,131,132,152,155,156,185,186
     17 */
    NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
    /**
     20 * 中国电信：China Telecom
     21 * 133,1349,153,180,189
     22 */
    //添加177与170的号码...
    NSString * CT = @"^1((33|53|7[07]|8[09])[0-9]|349)\\d{7}$";
    /**
     25 * 大陆地区固话及小灵通
     26 * 区号：010,020,021,022,023,024,025,027,028,029
     27 * 号码：七位或八位
     28 */
    //     NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    
    if (([regextestmobile evaluateWithObject:mobileNum] == YES)
        || ([regextestcm evaluateWithObject:mobileNum] == YES)
        || ([regextestct evaluateWithObject:mobileNum] == YES)
        || ([regextestcu evaluateWithObject:mobileNum] == YES)) {
        return YES;
    } else {
        return NO;
    }
}

#pragma mark - 邮箱格式验证
+ (BOOL)isValidateEmail:(NSString *)email
{
    NSString *emailRegex = @"[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",emailRegex];
    return [emailTest evaluateWithObject:email];
}

#pragma mark - 根据label文字内容与字体大小返回高度
+ (CGFloat)labelHeightWithString:(NSString *)string andFontSize:(CGFloat)fontSize andMaxWidth:(CGFloat)maxWidth andMaxHeight:(CGFloat)maxHeight
{
    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:string];
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.lineSpacing = 0.5;
    UIFont *font = [UIFont systemFontOfSize:fontSize];
    [attributeString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, string.length)];
    [attributeString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, string.length)];
    NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
    CGRect rect = [attributeString boundingRectWithSize:CGSizeMake(maxWidth, maxHeight) options:options context:nil];
    
    if ([self containEnglish:string]) {
        rect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height + style.lineSpacing);
    }else if ([self containChinese:string]){
        rect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height - style.lineSpacing);
    }
    return rect.size.height;
}

#pragma mark - 判断如果包含中文
+ (BOOL)containChinese:(NSString *)str {
    for(int i=0; i< [str length];i++){
        int a = [str characterAtIndex:i];
        if( a > 0x4e00 && a < 0x9fff){
            return YES;
        }
    }
    return NO;
}

#pragma mark - 判断如果包含英文
+ (BOOL)containEnglish:(NSString *)str {
    NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLKMNOPQRSTUVWXYZ"];
    if ([str rangeOfCharacterFromSet:set].location != NSNotFound) {
        return YES;
    }
    return NO;
}

/**
 *  账号验证
 */
+ (BOOL)isValidUserAccount:(NSString *)userAccount
{
    NSString * userAccountRegex = @"^[a-zA-Z0-9_]{6,20}$";
    NSPredicate *userAccountTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", userAccountRegex];
    return [userAccountTest evaluateWithObject:userAccount];
}

/**
 *  密码验证
 */
+ (BOOL)isValidPassWord:(NSString *)userPassword
{
    NSString * userPasswordRegex = @"^[a-zA-Z0-9_]{6,20}$";
    NSPredicate *userPasswordTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", userPasswordRegex];
    return [userPasswordTest evaluateWithObject:userPassword];
}

/**
 *  生日验证
 */
+ (BOOL)isValidBirthday:(NSString *)userBirthday
{
    NSString * birthdayRegex = @"^[0-9]{6,8}$";
    NSPredicate *birthdayTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", birthdayRegex];
    return [birthdayTest evaluateWithObject:userBirthday];
}

@end
